package com.tanhua.dubbo.api.mongo;

import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 视频服务接口实现
 */
@DubboService
@Slf4j
public class VideoApiImpl implements VideoApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    /**
     * 发布视频
     *
     * @param video
     */
    @Override
    public void addVideo(Video video) {
        video.setVid(idWorker.getNextId("video"));//自增id
        video.setCreated(System.currentTimeMillis());//发布视频时间
        mongoTemplate.insert(video);
    }

    /**
     * 查询视频列表按时间降序
     *
     * @param page
     * @param pageSize
     * @param vidList
     * @return
     */
    @Override
    public PageResult<Video> findPageVideo(Long page, Long pageSize, List<Long> vidList) {
        //1查询总记录数
        Query query = new Query();
        long counts = mongoTemplate.count(query, Video.class);
        //2当前页面数据
        if (!CollectionUtils.isEmpty(vidList)) {
            query.addCriteria(Criteria.where("vid").in(vidList));
        } else {
            query.limit(pageSize.intValue()).skip((page - 1) * pageSize);
        }
        //2.1 date降序
        query.with(Sort.by(Sort.Direction.DESC, "created"));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        //3返回PageResult
        return new PageResult<>(page, pageSize, counts, videoList);
    }

    /**
     * 更新視頻 點贊
     *
     * @param id
     */
    @Override
    public void updateVideo(ObjectId id, String path) {
        Video video = mongoTemplate.findById(id, Video.class);
        if (video != null) {
            Query query = new Query();
            query.addCriteria(Criteria.where("ObjectId").is(id));
            Update update = new Update();
            if (path.equals("like")) {
                update.set("likeCount", video.getLikeCount() + 1);
            }
            if (path.equals("dislike")) {
                update.set("likeCount", video.getLikeCount() - 1);
            }
            if (path.equals("userFocus")) {
                update.set("loveCount", video.getLikeCount() + 1);
            }
            if (path.equals("userUnFocus")) {
                update.set("loveCount", video.getLikeCount() -1);
            }
            if (path.equals("addComments")) {
                update.set("commentCount", video.getLikeCount() + 1);
            }
            if (path.equals("deleteComments")) {
                update.set("commentCount", video.getLikeCount() -1);
            }

            mongoTemplate.updateFirst(query, update, Video.class);
        } else {
            throw new TanHuaException("視頻不存在");
        }
    }
}
